package com.imooc.infra.repository;

import com.imooc.business.domain.Organ;
import com.imooc.business.domain.Storage;
import com.imooc.infra.mapping.StorageMapping;
import com.imooc.infra.repository.entity.TDataCenterOrgan;
import com.imooc.infra.repository.entity.TDataCenterStorage;
import com.imooc.infra.repository.service.ITDataCenterStorageService;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author Tim
 * @Description:
 * @Version 1.0
 */
@Repository
public class StorageRepository {
    
    @Setter(onMethod_ = @Autowired)
    private ITDataCenterStorageService storageService;
    @Setter(onMethod_ = @Autowired)
    private StorageMapping storageMapping;
    
    public Storage getByRuleId(Long ruleId) {
        TDataCenterStorage storage = storageService.getByRuleId(ruleId);
        return Optional.ofNullable(storage).map(storageMapping::toVo).orElse(null);
    }

    public Storage saveStorage(Long ruleId, Storage storage) {
        TDataCenterStorage tDataCenterStorage = storageMapping.toEntity(storage);
        tDataCenterStorage.setRule(ruleId);
        boolean save = storageService.save(tDataCenterStorage);
        return save ? storageMapping.toVo(tDataCenterStorage) : null;
    }
}
